﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Controller;
using Temp.Model;
using Temp.NLG;

namespace Temp.View
{
    class ItemView
    {

        /**Variables related to inventory**/
        private Texture2D inventoryImage;
        private Rectangle inventoryBox;
        private bool downAnimDone, upAnimDone, hidden;
        private Vector2 inventoryPos;
        /**Variables related to inventory**/

        private ContentManager Content;
        private StoryNode currentNode;

        private int curPlace;
        private MouseState mouse;
        private MouseState mousePast;
        private Rectangle mouseBox;


        private List<Texture2D> pics;

        /**Variables related to producing item description**/
        private Describer itemDesc;
        private List<Item> items;
        private List<Item> inventoryList;
        private DialogueView dialogueV;
        /**Variables related to producing item description**/


        private int X_OFFSET = 150;
        private int Y_OFFSET = -80;

        private ItemController itemC;

        public bool showing { get; set; }

        /// <summary>
        /// Constructor for ItemView class
        /// </summary>
        /// <param name="items">List of items in the world</param>
        /// <param name="dialogueV">Instance of the DialogueView class</param>
        /// <param name="currentNode">The current story node</param>
        /// <param name="inventoryList">List of items in the inventory</param>
        public ItemView(List<Temp.Model.Item> items, DialogueView dialogueV, StoryNode currentNode, List<Item> inventoryList)
        {
            this.items = items;
            itemC = new ItemController();
            pics = new List<Texture2D>();
            this.dialogueV = dialogueV;
            this.currentNode = currentNode;
            this.inventoryList = inventoryList;
            showing = false;
        }

        /// <summary>
        /// Initializes the variables to be used by the ItemView class
        /// </summary>
        public void Initialize()
        {
            inventoryBox = new Rectangle(624, 2, 80, 20);
            hidden = upAnimDone = downAnimDone = true;
            inventoryPos = new Vector2(3, -120);

            // mouseBox = new Rectangle(0, 0, 30, 30);

            //itemDesc = new Describer(currentNode);
        }

        /// <summary>
        /// Loads the images to be used by the ItemView class
        /// </summary>
        /// <param name="Content"></param>
        public void LoadContent(ContentManager Content)
        {
            foreach (Item it in items)
            {
                pics.Add(Content.Load<Texture2D>(it.pic));
            }
            this.Content = Content;
            inventoryImage = Content.Load<Texture2D>("UI elements/inventory");

        }

        /// <summary>
        /// Gets the mouse position
        /// </summary>
        protected void processMouse()
        {
            mouse = Mouse.GetState();
            if (ButtonState.Pressed == mouse.LeftButton)
            {

                mouseBox.X = mouse.X;
                mouseBox.Y = mouse.Y;
                intersectInventory();

                if (downAnimDone)
                {
                    checkInventoryItemClick();
                }
            }
            mousePast = mouse;
        }

        /// <summary>
        /// Checks whether the inventory box was clicked
        /// </summary>
        private void intersectInventory()
        {
            if (mouseBox.Intersects(inventoryBox) && mouse.LeftButton == ButtonState.Pressed && hidden && upAnimDone)
            {
                hidden = false;
                downAnimDone = false;
                showing = true;
            }
            else if (mouseBox.Intersects(inventoryBox) && mouse.LeftButton == ButtonState.Pressed && !hidden && downAnimDone)
            {
                hidden = true;
                upAnimDone = false;
                showing = false;

            }

            if (!hidden && ButtonState.Pressed == mouse.LeftButton && downAnimDone && mouse.Y >= 130)
            {
                hidden = true;
                upAnimDone = false;
                showing = false;

            }
            if (hidden && Y_OFFSET == -80)
                showing = false;
            
        }

        //private void intersectItems()
        //{
            //foreach (Temp.Model.Item o in items)
            //{
            //    Texture2D t = content.Load<Texture2D>(o.pic);
            //    if (mouseBox.Intersects(new Rectangle(o.x, o.y, t.Width, t.Height)) && mouse.LeftButton == ButtonState.Pressed && mousePast.LeftButton == ButtonState.Released)
            //    {
            //        System.Diagnostics.Debug.WriteLine("ITEM CLICKED");
            //        //ItemPlanner ip = new ItemPlanner(dialogueV, currentNode);
            //        //ip.renderItemDescription(o.name);
            //        //dialogueV.char1Pic = content.Load<Texture2D>("Peer/Boy/peer");
            //        //Conversation c = new Conversation("Alex", itemDesc.getItemDescription(o.id), 'I');//, null);
            //        //dialogueV.listDialogue.Clear();
            //        //dialogueV.listDialogue.Add(c);
            //        //dialogueV.modifyListDialogue();
            //        //dialogueV.isShowing = true;
            //    }
            //}
            //mousePast = mouse;
        //}

        /// <summary>
        /// Animates the inventory to move downwards 
        /// </summary>
        private void inventoryDownAnimation()
        {
            if (inventoryPos.Y <= 10)
            {
                inventoryPos.Y += 5;
            }
            else
            {
                inventoryBox.Y = 130;
                downAnimDone = true;
            }

            if (Y_OFFSET <= 50)
            {
                Y_OFFSET += 5;
            }
            else
            {
                Y_OFFSET = 50;
            }
        }

        /// <summary>
        /// Animates the inventory to move upwards
        /// </summary>
        private void inventoryUpAnimation()
        {
            if (inventoryPos.Y >= -120)
            {
                inventoryPos.Y -= 5;

            }
            else
            {
                inventoryBox.Y = 2;
                upAnimDone = true;
            }

            if (Y_OFFSET >= -50)
            {
                Y_OFFSET -= 5;
            }
            else
            {
                Y_OFFSET = -80;
            }
        }

        /// <summary>
        /// Calls the processmouse,inventoryDownAnimation and inventoryUpAnimation when needed
        /// </summary>
        /// <param name="worldNum"></param>
        public void Update(int worldNum)
        {
            this.curPlace = worldNum;
            processMouse();
            if (!downAnimDone && upAnimDone)
            {
                inventoryDownAnimation();
            }
            else if (!upAnimDone && downAnimDone)
            {
                inventoryUpAnimation();
            }

            
        }

        /// <summary>
        /// Draws the items in the inventory
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void DrawItems(SpriteBatch spriteBatch)
        {
            int i = 0;
            foreach (Item it in items)
            {
                if (curPlace == it.idPlace)
                {
                    System.Diagnostics.Debug.WriteLine("ITEMS IN PLACE: " + it.name);
                    if (it.name == "phone") {
                        System.Diagnostics.Debug.WriteLine("phone X:" + it.x + " phone Y:" + it.y);
                    }
                    spriteBatch.Draw(Content.Load<Texture2D>(it.pic), new Rectangle(it.x, it.y, it.dimX, it.dimY), Color.White);
                }
                i++;
            }
        }

        /// <summary>
        /// Draws the inventory box
        /// </summary>
        /// <param name="spriteBatch">Instance of the SpriteBatch class</param>
        public void DrawInv(SpriteBatch spriteBatch)
        {

            int ctr = 0;
            spriteBatch.Draw(inventoryImage, inventoryPos, Color.White);

            foreach (Item inv in inventoryList)
            {
                inv.x = 63 + ctr * X_OFFSET;
                inv.y = Y_OFFSET;
                spriteBatch.Draw(Content.Load<Texture2D>(inv.pic), new Vector2((float)(63 + ctr * X_OFFSET), (float)Y_OFFSET), Color.White);
                ctr++;
            }

        }

        /// <summary>
        /// get and set method for the Items variable
        /// </summary>
        public List<Item> Items
        {
            get { return items; }
            set
            {
                items = value;
                pics.Clear();
                foreach (Item it in items)
                {
                    pics.Add(Content.Load<Texture2D>(it.pic));
                }
            }
        }

        /// <summary>
        /// get and set method for the curPlace variable
        /// </summary>
        public int CurPlace
        {
            get { return curPlace; }
            set { curPlace = value; }
        }

        /// <summary>
        /// get and set method for the inventory variable
        /// </summary>
        public List<Item> Inventory
        {
            get { return inventoryList; }
            set { inventoryList = value; }

        }

        private void checkInventoryItemClick() {
            foreach (Item inv in inventoryList) {
                if (mouseBox.Intersects(new Rectangle(inv.x, inv.y, inv.dimX, inv.dimY)) && mouse.LeftButton == ButtonState.Pressed && mousePast.LeftButton == ButtonState.Released)
                {
                    System.Diagnostics.Debug.WriteLine("result" + keyItem(inv));
                    if (keyItem(inv))
                    {
                        Conversation c = new Conversation("Alex", "We cant leave this item because it is important.", 'I');
                        List<Conversation> listC = new List<Conversation>();
                        listC.Add(c);
                        dialogueV.listDialogue = listC;
                        dialogueV.modifyListDialogue();
                        dialogueV.char2Pic = null;
                        dialogueV.isShowing = true;
                    }
                    else{
                        Conversation c = new Conversation("Alex", "Do you want us to leave behind the " + inv.name, 'I');
                        List<string> optionList = new List<string>();
                        optionList.Add("Leave Item");
                        optionList.Add("Keep Behind");
                        c.optionList = optionList;
                        c.nameOfTriggerer = inv.name;
                        List<Conversation> listC = new List<Conversation>();
                        listC.Add(c);
                        dialogueV.listDialogue = listC;
                        dialogueV.modifyListDialogue();
                        dialogueV.char2Pic = null;
                        dialogueV.isShowing = true;
                        System.Diagnostics.Debug.WriteLine("ITEM INVENTORY PICKED:" + inv.name);
                    }
                }
            }
        }

        private bool keyItem(Item inv) {
            Ontology keyItem = itemC.checkKeyItem(inv.name);
            System.Diagnostics.Debug.WriteLine("IN KEY ITEM"+keyItem.concept2);
            if (keyItem != null) {
                return true;
            }
            return false;
        }
    }
}
